<template>
	<view class="account-login-container">
		<view class="form-header">
			<u-icon name="arrow-left" size="40" @click="goBack"></u-icon>
			<text class="title">手机号登录</text>
		</view>
		
		<view class="login-form">
			<u-form :model="form" ref="uForm">
				<u-form-item label="手机号" prop="studentPhone">
					<u-input v-model="form.studentPhone" placeholder="请输入手机号" />
				</u-form-item>
				<u-form-item label="密码" prop="studentPwd">
					<u-input v-model="form.studentPwd" type="password" placeholder="请输入密码" />
				</u-form-item>
				<u-form-item label="验证码" prop="code">
					<view class="captcha-container">
						<u-input v-model="form.code" placeholder="请输入验证码" />
						<image class="captcha-image" :src="image" mode="aspectFit" @click="refreshCaptcha"></image>
					</view>
				</u-form-item>
			</u-form>
		</view>
		
		<view class="login-buttons">
			<u-button type="primary" @click="handleLogin">登录</u-button>
		</view>
	</view>
</template>

<script>
import { getCode, login } from '@/api/login.js'
	export default {
		data() {
			return {
				form: {
					studentPhone: '',
					studentPwd: '',
					code: ''
				},
				key: '',
				image: '',
				rules: {
					studentPhone: [{
						required: true,
						message: '请输入手机号',
						trigger: ['blur', 'change']
					}, {
						pattern: /^1[3-9]\d{9}$/,
						message: '请输入正确的手机号',
						trigger: ['blur', 'change']
					}],
					studentPwd: [{
						required: true,
						message: '请输入密码',
						trigger: ['blur', 'change']
					}],
					code: [{
						required: true,
						message: '请输入验证码',
						trigger: ['blur', 'change']
					}]
				}
			}
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
			this.handleCode();
		},
		methods: {
			goBack() {
				uni.navigateBack();
			},
			refreshCaptcha() {
				this.handleCode();
			},
			handleLogin() {
				this.$refs.uForm.validate().then(valid => {
					if (valid) {
						console.log('登录表单', this.form);
						this.form.uuid = this.key;
						login(this.form).then(res => {
							console.log(res)
							uni.setStorageSync('studentId', res.data.login.studentId);
							uni.setStorageSync('campusId', res.data.login.campusId);
							uni.setStorageSync('token', res.data.token);
							uni.showToast({
								title: '登录成功',
								icon: 'success'
							});
							uni.switchTab({
								url: '/pages/main/main'
							});
						})
					} else {
						console.log('表单验证失败');
					}
				}).catch(errors => {
					console.log('表单验证错误', errors);
				});
			},
			handleCode() {
				this.key = Date.now();
				getCode(this.key).then(res => {
					console.log(res)
					this.image = "data:image/png;base64," + res.data.base64;
				})
			},
		}
	}
</script>

<style lang="scss">
.account-login-container {
	padding: 40rpx;
	min-height: 100vh;
	background-color: #fff;
	
	.form-header {
		display: flex;
		align-items: center;
		margin-bottom: 60rpx;
		
		.title {
			font-size: 36rpx;
			font-weight: bold;
			margin-left: 30rpx;
		}
	}
	
	.login-form {
		margin-top: 40rpx;
		
		:deep(.u-form-item) {
			.u-form-item__body {
				.u-form-item__body__left {
					width: 160rpx;
				}
				.u-form-item__body__right {
					width: calc(100% - 160rpx);
				}
			}
		}
	}
	
	.captcha-container {
		display: flex;
		align-items: center;
		gap: 20rpx;
		
		.u-input {
			width: 300rpx;
		}
		
		.captcha-image {
			width: 240rpx;
			height: 100rpx;
			border-radius: 8rpx;
		}
	}
	
	.login-buttons {
		margin-top: 60rpx;
	}
}
</style>

